/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.to.aboomy.bannersample.indicator;

import com.to.aboomy.banner.Indicator;
import com.to.aboomy.bannersample.indicator.magicindicator.ColorFlipPagerTitleView;
import com.to.aboomy.bannersample.indicator.magicindicator.CommonNavigator;
import com.to.aboomy.bannersample.indicator.magicindicator.CommonNavigatorAdapter;
import com.to.aboomy.bannersample.indicator.magicindicator.IpagerIndicator;
import com.to.aboomy.bannersample.indicator.magicindicator.IpagerTitleView;
import com.to.aboomy.bannersample.indicator.magicindicator.LinePagerIndicator;
import com.to.aboomy.bannersample.indicator.magicindicator.MagicIndicator;
import com.to.aboomy.bannersample.indicator.magicindicator.SimplePagerTitleView;
import com.to.aboomy.bannersample.indicator.magicindicator.UiUtil;
import ohos.agp.animation.Animator;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DependentLayout;
import ohos.agp.utils.Color;
import ohos.app.Context;

import java.util.Arrays;
import java.util.List;

/**
 * LinePagerTitleIndicatorView
 *
 * @since 2021-08-09
 */
public class LinePagerTitleIndicatorView extends MagicIndicator implements Indicator {
    private static final int NUM_3 = 3;
    private static final int NUM_6 = 6;
    private static final int NUM_10 = 10;
    private static final int NUM_100 = 100;
    private static final float NUM_F_0_5 = 0.5f;
    private static final float NUM_F_0_65 = 0.65f;
    private static final String[] CHANNELS = new String[]{"CUPCAKE", "DONUT", "ECLAIR", "GINGERBREAD", "HONEYCOMB"};
    DependentLayout.LayoutConfig params = null;
    private int pagerCount;
    private CommonNavigator commonNavigator;
    private List<String> mDataList = Arrays.asList(CHANNELS);
    private Context context;

    /**
     * 构造
     *
     * @param context
     */
    public LinePagerTitleIndicatorView(Context context) {
        this(context, null);
    }

    /**
     * 构造
     *
     * @param context
     * @param attrSet
     */
    public LinePagerTitleIndicatorView(Context context, AttrSet attrSet) {
        super(context, attrSet);
        commonNavigator = new CommonNavigator(context);
        this.context = context;
    }

    @Override
    public void initIndicatorCount(int pagerCount1) {
        this.pagerCount = pagerCount1;
        commonNavigator.setScrollPivotX(NUM_F_0_65);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return pagerCount;
            }

            @Override
            public IpagerTitleView getTitleView(Context context1, int index) {
                SimplePagerTitleView simplePagerTitleView = new ColorFlipPagerTitleView(context1);
                simplePagerTitleView.setText(mDataList.get(index));
                simplePagerTitleView.setNormalColor(Color.getIntColor("#9e9e9e"));
                simplePagerTitleView.setSelectedColor(Color.getIntColor("#FF0000"));
                return simplePagerTitleView;
            }

            @Override
            public IpagerIndicator getIndicator(Context context1) {
                LinePagerIndicator indicator = new LinePagerIndicator(context1);
                indicator.setMode(LinePagerIndicator.MODE_EXACTLY);
                indicator.setLineHeight(UiUtil.vp2px(context1, NUM_6));
                indicator.setLineWidth(UiUtil.vp2px(context1, NUM_10));
                indicator.setRoundRadius(UiUtil.vp2px(context1, NUM_3));
                indicator.setStartInterpolator(Animator.CurveType.ACCELERATE);
                indicator.setEndInterpolator(Animator.CurveType.DECELERATE);
                indicator.setColors(Color.getIntColor("#FF0000"));
                return indicator;
            }
        });
        setNavigator(commonNavigator);
    }

    @Override
    public Component getView() {
        return this;
    }

    @Override
    public DependentLayout.LayoutConfig getParams() {
        if (params == null) {
            params = new DependentLayout.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_CONTENT, NUM_100);
            params.addRule(DependentLayout.LayoutConfig.ALIGN_PARENT_BOTTOM);
            params.addRule(DependentLayout.LayoutConfig.CENTER_IN_PARENT);
            params.setMarginBottom(AttrHelper.vp2px(NUM_10, context));
        }
        return params;
    }

    @Override
    public void setAutoStau(Boolean isAuto) {
    }

    @Override
    public void onPageSliding(int position, float positionOffset, int positionOffsetPixels) {
        if (position != pagerCount - 1) {
            onPageScrolled(position, positionOffset, positionOffsetPixels);
        } else {
            if (positionOffset > NUM_F_0_5) {
                onPageScrolled(0, 0, 0);
            } else {
                onPageScrolled(position, 0, 0);
            }
        }
    }

    @Override
    public void onPageSlideStateChanged(int state) {
        onPageScrollStateChanged(state);
    }

    @Override
    public void onPageChosen(int itemPos) {
        onPageSelected(itemPos);
    }
}